$(document).ready(function(){

	$("#nip_ganti").keyup(function() {
        var val=$('#nip_ganti').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_ganti').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_ganti').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });
    $("#nip_atasan").keyup(function() {
        var val=$('#nip_atasan').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_langsung').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_langsung').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });
    $("#nip_setuju").keyup(function() {
        var val=$('#nip_setuju').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_setuju').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_setuju').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });

	$("#btn_simpan").click(function(){
		$.post('controller/cont.cuti_ln.php',$("#frm_buat").serialize(),function(data){
			if(data.msg){
				if(data.buat){
					document.getElementById("frm_aksi").value = "simpan";
					$("#nip_detail").html(data.cuti.pegawai.nip);
					$("#nama_detail").html(data.cuti.pegawai.nama);
					$("#tgl_detail").html(data.cuti.tglAwal +" s.d. "+data.cuti.tglAkhir);
					$("#jns_detail").html(data.cuti.idJns);
					goAdd();
				}else{
					document.getElementById("sukses").innerHTML = data.sukses;
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    loadCuti();
					goUndo();
				}
			}else{
				document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
			}
		},'json');
		return false;
	});

	loadCuti();
	
});

function loadCuti(){
	$.getJSON('controller/cont.cuti_ln.php',{aksi_get_cuti_ln:true},function(data){
		$('#list_pegawai tbody').html('');
        var tab = '';
        if (data.msg) {
            $.each(data.cuti, function(index, cuti) {
                if(cuti.cutinya!==false){
                    tab += "<tr>";
                    tab += "<td>" + cuti.pegawai.nip + "</td>";
                    tab += "<td>" + cuti.pegawai.nama + "</td>";
                    tab += "<td>"+cuti.tglAwal+" s.d. "+cuti.tglAkhir+" </td>";
                    tab += "<td>" + cuti.idJns + "</td>";
                    tab += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + cuti.id + "\"/></td>";
                    tab += "</tr>";
                }
            });
        } else {
            tab += "<tr>"
            tab += "<td colspan=\"5\">Data Tidak Ada</td>";
            tab += "</tr>";
        }
        $('#list_pegawai tbody').append(tab);
	});
}